package com.etone.app.crawler.portals.handle;

import com.etone.app.crawler.portals.entity.OutputEntity;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.log4j.Logger;

/**
 *
 * @author <a href="mailto:cbean@qq.com">CowboyBean</a>
 * @date Mar 14, 2014
 * @version $$Reversion$$
 */
public class BaseHandle extends Thread {

    private static final Logger logger = Logger.getLogger(BaseHandle.class);

    private static FileWriter writer = null;
    private static int fileIndex = 0;
    private static long outputCounter = 0;

    /**
     * 新建一个writer
     */
    private static void newWriter() {

        try {
            writer = new FileWriter(new File("z:/protals_" + fileIndex + ".csv"));
            fileIndex++;
            writer.write("ID | 业务名称 | 业务URL | 当前URL | 主机URL | 当前页面等级 | 连接来源页 | UserAgent | 处理结果 | Title | Keywords | Description | text \n");
            writer.flush();
        } catch (Exception ex) {
            logger.error("新建输出文件失败！", ex);
        } finally {

        }
    }

    /**
     * 关闭writer
     */
    private static void closeWriter() {
        if (writer != null) {
            try {
                writer.flush();
                writer.close();
            } catch (Exception ex) {
                // Ignore;
            }
        }
    }

    /**
     * 向输出文件写指定的内容
     *
     * @param msg
     * @return
     */
    private synchronized static boolean write(String msg) {

        boolean result = false;

        if (writer == null) {
            newWriter();
        }
        try {
            writer.write(outputCounter + "|" + msg + "\n");
            outputCounter++;
            writer.flush();
            // 每1W条记录换一个新文件
            if (outputCounter % 10000 == 0) {
                closeWriter();
                newWriter();
            }
            result = true;
        } catch (IOException ex) {
            logger.error("写入输出文件时失败！", ex);
        }

        return result;
    }

    protected static boolean write(OutputEntity entity) {

        boolean result = false;

        // ID | 业务名称 | 业务URL | 当前url | 当前主机 | 当前页面等级 | 连接来源页 
        // | UserAgent | 处理结果 | Title | Keywords | Description | text
        write(entity.getServiceName() + "|"
                + entity.getServiceUrl() + "|"
                + entity.getUrl() + "|"
                + entity.getHost() + "|"
                + entity.getLevel() + "|"
                + entity.getSrcUrl() + "|"
                + entity.getUserAgent() + "|"
                + entity.getHttpCode() + "|"
                + entity.getTitle() + "|"
                + entity.getKeywords() + "|"
                + entity.getDescription() + "|"
                + entity.getText()
        );

        return result;

    }

}
